package com.orm.mybatis.serviceImpl;


import com.orm.mybatis.entity.AsusPoInfo;
import com.orm.mybatis.entity.User;
import com.orm.mybatis.mapper.master1.UserMapper1;
import com.orm.mybatis.mapper.master2.UserMapper2;
import com.orm.mybatis.mapper.master3.AsusPoInfoMapper3;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class UserServiceImpl {

    @Resource
    private UserMapper1 userMapper1;

    @Resource
    private UserMapper2 userMapper2;

    @Resource
    private AsusPoInfoMapper3 asusPoInfoMapper3;

    public List<User> findAllUser(){
        List<User> list = userMapper1.selectAllUser();
        return list;
    }

    public List<User> findAllUser1(){
        List<User> list = userMapper2.selectAllUser();
        return list;
    }

    public User findUserById(Long id){
        return userMapper1.selectUserById(id);
    }

    public int insertUser(User user){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setCreateTime(simpleDateFormat.format(new Date()));
        user.setLastLoginTime(simpleDateFormat.format(new Date()));
        user.setLastUpdateTime(simpleDateFormat.format(new Date()));
        return userMapper1.saveUser(user);
    }

    @Transactional
    public void testTransitional() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date =  simpleDateFormat.format(new Date());
        String UUID = java.util.UUID.randomUUID().toString().substring(0,5);
        User user = User.builder().email("andrew@qq.com"+UUID).name("andrew"+UUID).password("123456"+UUID).phoneNumber("123"+UUID)
                .lastUpdateTime(date).createTime(date).status(0).salt("password"+UUID).build();
        AsusPoInfo asusPoInfo = AsusPoInfo.builder().id(java.util.UUID.randomUUID().toString().substring(0,20))
                .woNo("andrew").po("123456").poLine("poline").cPo("cpo123456").shipType("Direct").build();
        userMapper1.saveUser(user);
        userMapper2.saveUser(user);
        asusPoInfoMapper3.insertAsusPoInfo(asusPoInfo);
//        throw new RuntimeException();
    }

    public void test() throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date =  simpleDateFormat.format(new Date());
        String UUID = java.util.UUID.randomUUID().toString().substring(0,5);
        User user = User.builder().email("andrew@qq.com"+UUID).name("andrew"+UUID).password("123456"+UUID).phoneNumber("123"+UUID)
                .lastUpdateTime(date).createTime(date).status(0).salt("password"+UUID).build();
        try {
             testNoXml();
        }catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }finally {
            System.out.println("finally");
        }
        System.out.println("还会跑吗");
        userMapper1.saveUser(user);
        File file = new File("D:/车载/oookk/wawa");
        FileOutputStream outputStream = new FileOutputStream(file);
        System.out.println("还能继续跑吗");
    }

    @Transactional
    public void  testNoXml() throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date =  simpleDateFormat.format(new Date());
        String UUID = java.util.UUID.randomUUID().toString().substring(0,5);
        User user = User.builder().email("andrew@qq.com"+UUID).name("andrew"+UUID).password("123456"+UUID).phoneNumber("123"+UUID)
                .lastUpdateTime(date).createTime(date).status(0).salt("password"+UUID).build();
        userMapper1.saveUser(user);
        try {
            int i = 1/0;
        }catch (Exception e){
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new RuntimeException();
//         return ;
        }
        System.out.println("在testNoXml内还会跑吗");
        return ;
    }

    public List<AsusPoInfo> selectOracle(){
       return asusPoInfoMapper3.selectAllAsusPoInfo();
    }

    public void insertOracle(AsusPoInfo asusPoInfo){
        asusPoInfoMapper3.insertAsusPoInfo(asusPoInfo);
    }

}
